
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from models import Song

def get_song_key(lang):
    if lang=='e':
        parent='englishsong'
    else:
        parent='hindisong'
    return db.Key.from_path(parent, lang)

class AddSong(webapp.RequestHandler):
    def get(self):
        english_songs = ['who the fuck is alice',
                 'once',
                 'sentimental',
                 'no surprises',
                 'thinking about you',
                 'trains',
                 'adele - someone like you',
                 'when i am not there',
                 'beautiful killer',
                 'dil dhadak-dhadak',
                 'sex in the city',
                 'highway to hell',]
        
        hindi_songs = ['senorita',
                       'hawa hawaei',
                       'rubaru',
                       'jane tu na jane naa']

        self.response.headers['Content-Type']="text/plain"
        self.response.out.write('English Songs: \n')
        
        for song in english_songs:
            self.response.out.write('Adding English song '+song)
            self.response.out.write('\n')
            Song(parent=get_song_key('e'), songname=song,timesplayed=0 ).put()

        for song in hindi_songs:
            self.response.out.write('Adding Hindi song '+song)
            self.response.out.write('\n')
            Song(parent=get_song_key('h'), songname=song, timesplayed=0).put()


class ShowSong(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type']='text/plain'
        
        english_songs=Song.all().ancestor(get_song_key('e')).fetch(1000)
        hindi_songs=Song.all().ancestor(get_song_key('h')).fetch(1000)
        
        self.response.out.write('Displaying English Songs: \n\n')
        for song in english_songs:
            self.response.out.write(song.songname+'\n')

        self.response.out.write('\n\nDisplaying Hindi Songs: \n\n')
        for song in hindi_songs:
            self.response.out.write(song.songname+'\n')
        
class ShowPlaylist(db.Model):
    def get(self):
        self.response.headers['Content-Type']='text/plain'
        uip=UserInPlaylist.all()
        for u in uip:
            self.response.out.write(u.user.nickname()+':\n')
            us=SongInPlaylist.all()
            
            
            
def main():
    app=webapp.WSGIApplication([('/addsongs', AddSong),
                                ('/showsongs', ShowSong),
                                ('/showplaylist', ShowPlaylist)], debug=True)
    run_wsgi_app(app)

    
if __name__=="__main__":
    main()
